Imports System.Xml
Imports System.Globalization
Imports System.Collections.Generic

Namespace Common.Rss.Version20
    Public Class Channel
        'Required
        Dim _Title As String
        Dim _Link As Uri
        Dim _Description As String
        'Optional
        Dim _Language As CultureInfo
        Dim _Copyright As String
        Dim _ManagingEditor As String
        Dim _WebMaster As String
        Dim _PublicationDate As Date
        Dim _LastBuildDate As Date
        Dim _Category As String
        Dim _AssemblyInfo As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly
        ReadOnly _Generator As String = _AssemblyInfo.GetName.Name & "Rss 2.0 Generator v" & _AssemblyInfo.GetName.Version.ToString
        ReadOnly _Docs As Uri = New Uri("http://blogs.law.harvard.edu/tech/rss/")
        Dim _TimeToLive As Integer
        Dim _Image As Image
        Dim _Items As List(Of Item)

        Public ReadOnly Property Items() As List(Of Item)
            Get
                Return _Items
            End Get
        End Property
        Public Property Title() As String
            Get
                Return _Title
            End Get
            Set(ByVal Value As String)
                _Title = Value
            End Set
        End Property
        Public Property Link() As Uri
            Get
                Return _Link
            End Get
            Set(ByVal Value As Uri)
                _Link = Value
            End Set
        End Property
        Public Property Description() As String
            Get
                Return _Description
            End Get
            Set(ByVal Value As String)
                _Description = Value
            End Set
        End Property
        Public Property Language() As CultureInfo
            Get
                Return _Language
            End Get
            Set(ByVal Value As CultureInfo)
                _Language = Value
            End Set
        End Property
        Public Property Copyright() As String
            Get
                Return _Copyright
            End Get
            Set(ByVal Value As String)
                _Copyright = Value
            End Set
        End Property
        Public Property ManagingEditor() As String
            Get
                Return _ManagingEditor
            End Get
            Set(ByVal Value As String)
                _ManagingEditor = Value
            End Set
        End Property
        Public Property Webmaster() As String
            Get
                Return _WebMaster
            End Get
            Set(ByVal Value As String)
                _WebMaster = Value
            End Set
        End Property
        Public Property PublicationDate() As Date
            Get
                Return _PublicationDate
            End Get
            Set(ByVal Value As Date)
                _PublicationDate = Value.ToUniversalTime
            End Set
        End Property
        Public Property LastBuildDate() As Date
            Get
                Return _LastBuildDate
            End Get
            Set(ByVal Value As Date)
                _LastBuildDate = Value.ToUniversalTime
            End Set
        End Property
        Public Property Category() As String
            Get
                Return _Category
            End Get
            Set(ByVal Value As String)
                _Category = Value
            End Set
        End Property
        Public ReadOnly Property Generator() As String
            Get
                Return _Generator
            End Get
        End Property
        Public ReadOnly Property Documentation() As Uri
            Get
                Return _Docs
            End Get
        End Property
        Public Property TimeToLive() As Integer
            Get
                Return _TimeToLive
            End Get
            Set(ByVal Value As Integer)
                _TimeToLive = Value
            End Set
        End Property
        Public Property Image() As Image
            Get
                Return _Image
            End Get
            Set(ByVal value As Image)
                _Image = value
            End Set
        End Property
        Sub New()
            _Items = New List(Of Item)
            _Link = New Uri("http://localhost")
            _Language = New CultureInfo("en-US")
            _PublicationDate = Date.Now.ToUniversalTime
            _LastBuildDate = Date.Now.ToUniversalTime
        End Sub
        Sub New(ByVal element As XmlElement)
            _Title = element.SelectSingleNode("//title").InnerText()
            _Link = New Uri(element.SelectSingleNode("//link").InnerText)
            _Description = element.SelectSingleNode("//description").InnerText()
            _Language = New CultureInfo(element.SelectSingleNode("//language").InnerText())
            _Copyright = element.SelectSingleNode("//copyright").InnerText
            _WebMaster = element.SelectSingleNode("//webMaster").InnerText
            _ManagingEditor = element.SelectSingleNode("//managingEditor").InnerText
            _PublicationDate = Date.Parse(element.SelectSingleNode("//pubDate").InnerText).ToLocalTime
            _LastBuildDate = Date.Parse(element.SelectSingleNode("//lastBuildDate").InnerText)
            _Category = element.SelectSingleNode("//category").InnerText
            _TimeToLive = element.SelectSingleNode("//ttl").InnerText
            PopulateItems(element)
        End Sub
        Public Function ToXMLElement(ByVal document As XmlDocument) As XmlElement
            Dim xElement As XmlElement = document.CreateElement("channel")
            xElement.AppendChild(CreateXMLElement(document, "title", _Title))
            xElement.AppendChild(CreateXMLElement(document, "link", _Link.ToString))
            xElement.AppendChild(CreateXMLElement(document, "description", _Description))
            xElement.AppendChild(CreateXMLElement(document, "language", _Language.TwoLetterISOLanguageName))
            xElement.AppendChild(CreateXMLElement(document, "copyright", _Copyright))
            xElement.AppendChild(CreateXMLElement(document, "managingEditor", _ManagingEditor))
            xElement.AppendChild(CreateXMLElement(document, "webMaster", _WebMaster))
            xElement.AppendChild(CreateXMLElement(document, "pubDate", _PublicationDate.ToString("r")))
            xElement.AppendChild(CreateXMLElement(document, "lastBuildDate", _LastBuildDate.ToUniversalTime.ToString("r")))
            xElement.AppendChild(CreateXMLElement(document, "generator", _Generator))
            xElement.AppendChild(CreateXMLElement(document, "docs", _Docs.ToString))
            xElement.AppendChild(CreateXMLElement(document, "ttl", _TimeToLive))
            If _Image IsNot Nothing Then
                xElement.AppendChild(_Image.ToXMLElement(document, Me))
            End If
            InsertItems(xElement, document)
            Return xElement
        End Function
        Private Sub PopulateItems(ByVal element As XmlElement)
            _Items = New List(Of Item)
            Dim xl As XmlNodeList = element.SelectNodes("//item")
            Dim x As XmlNode
            For Each x In xl
                _Items.Add(New Item(x))
            Next
        End Sub
        Private Sub InsertItems(ByVal buffer As XmlElement, ByVal document As XmlDocument)
            Dim obj As Object
            For Each obj In _Items
                Dim m_Temp As Item = obj
                buffer.AppendChild(m_Temp.ToXMLElement(document))
            Next
        End Sub

        Public Overrides Function ToString() As String
            Return Me.ToXMLElement(New XmlDocument()).OuterXml
        End Function
        Private Function CreateXMLElement(ByVal document As XmlDocument, ByVal name As String, ByVal value As String) As XmlElement
            Dim m_Buffer As XmlElement = document.CreateElement(name)
            m_Buffer.InnerText = value
            Return m_Buffer
        End Function
    End Class
End Namespace